package com.lwgk;
import android.app.Application;
import android.content.Context;
import com.lwgk.network.OkGoUtils;
import com.lwgk.utils.LifecycleBackgroundUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import me.jessyan.autosize.AutoSize;
import me.jessyan.autosize.AutoSizeConfig;

/**
 * created by 赵航
 * 联系微信 微信号：zhao--hang--
 * on 2019/7/10
 * --------------------------------------------------------------------------
 * 描述：该类为初始化类
 * 注意事项： 该类为全局有效，进行初始化一些代码
 */
public class AppComplication extends Application {

    //全局共用上下文，注意sp保存可用，
    public static Context mContext;

    //记录全局的状态栏高度，防止basic类重复获取，降低性能
    public static int mActionBarSize = 0;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;

        //今日头条适配方案， implements CustomAdapt可以去掉适配
        AutoSizeConfig.getInstance().setCustomFragment(true);
        AutoSize.initCompatMultiProcess(this);

        //部分手机提示，升级到 Android P 后 每次进入程序都会弹一个提醒弹窗
        closeAndroidPDialog();

        //初始化网络请求
        OkGoUtils.getInstance(AppComplication.this);

        //判断App是否在后台运行
        registerActivityLifecycleCallbacks(new LifecycleBackgroundUtils());
    }

    private void closeAndroidPDialog() {
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
